package com.duke.shaking.interaction;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.androidquery.AQuery;
import com.duke.shaking.R;
import com.duke.shaking.pojo.ChatFriend;
import com.duke.shaking.pojo.User;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChatFriendsListAdatper extends BaseAdapter {

	private LayoutInflater inflater;
	private AQuery aQuery;
	
	private Context context;
	private List<ChatFriend> friends;
	public ChatFriendsListAdatper(Context context, List<ChatFriend> friends) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.friends = friends;
		aQuery = new AQuery(context);
		
	}
	
	public int getCount() {
		if (friends == null) {
			return 0;
		}
		else {
			return friends.size();
		}
	}

	public Object getItem(int position) {
		if (friends == null || position >= friends.size()) {
			return null;
		}
		else {
			return friends.get(position);
		}
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	private void setVipIcon(User user, View vip, View vipYear) {
    	if (user == null || user.getLevel() == null || user.getLevel() <= 0) {
    		if (vip != null) {
    			vip.setVisibility(View.GONE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.GONE);
    		}
    	}
    	else if (user.getLevel() < User.level_vip_year) {
    		if (vip != null) {
    			vip.setVisibility(View.VISIBLE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.GONE);
    		}
    	}
    	else {
    		if (vip != null) {
    			vip.setVisibility(View.GONE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.VISIBLE);
    		}
    	}
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		FriendsViewHolder holder = null;
        if (convertView == null) {
             
            holder = new FriendsViewHolder();  
             
            convertView = inflater.inflate(R.layout.view_chatfriend_listitem, null);
            holder.img_avatar = (ImageView)convertView.findViewById(R.id.img_avatar);
            holder.txt_nickname = (TextView)convertView.findViewById(R.id.txt_nickname);
            holder.txt_profile = (TextView)convertView.findViewById(R.id.txt_profile);
            holder.txt_unreadMsg = (TextView)convertView.findViewById(R.id.txt_unreadMsg);
            holder.txt_timestamp = (TextView)convertView.findViewById(R.id.txt_timestamp);
            holder.img_vip = (ImageView)convertView.findViewById(R.id.img_vip);
            holder.img_vipYear = (ImageView)convertView.findViewById(R.id.img_vipyear);
            convertView.setTag(holder);
             
        }else {
            holder = (FriendsViewHolder)convertView.getTag();
        }
        
        
        if (position < 0 || friends == null || position >= friends.size()) {
        	return null;
        }
        
        ChatFriend friend = (ChatFriend)friends.get(position);
        if (friend.getAvatarUrl() == null) {
        	holder.img_avatar.setBackgroundResource(R.drawable.blank_avatar);
        }
        else {
        	aQuery.id(holder.img_avatar).image(friend.getAvatarUrl(), 
					true, true, 0, R.drawable.blank_avatar,null, 0);
        }
        holder.txt_nickname.setText(friend.getNickname());
        String sexStr = "";
        if (friend.getSex() == User.sex_female) {
        	sexStr = context.getString(R.string.female);
        }
        else if (friend.getSex() == User.sex_male) {
        	sexStr = context.getString(R.string.male);
        }
        
        int age = 0;
        if (friend.getAge() != null) {
        	age = friend.getAge();
        }
        else if (friend.getBirth() != null){
        	age = (int)((friend.getBirth().getTime() - (new Date()).getTime()) / 1000L / (24 * 3600 * 365));
        }
        if (age < 0) {
        	age = 0;
        }
        String profile = String.format(Locale.CHINA,
        		context.getString(R.string.fmt_age_and_location), 
        		age,
        		friend.getCity1(),
        		sexStr
        		);
        
        holder.txt_profile.setText(profile);
        String tipUnread = "";
        if (friend.getUnreadMessages() > 0) {
        	sexStr = "";
        	if (friend.getSex() == User.sex_female) {
            	sexStr = context.getString(R.string.she);
            }
            else if (friend.getSex() == User.sex_male) {
            	sexStr = context.getString(R.string.he);
            }
        	tipUnread = String.format(Locale.CHINA,
        			context.getString(R.string.tip_unread_msg), 
        			sexStr, friend.getUnreadMessages());
        }
        
        holder.txt_unreadMsg.setText(tipUnread);
        if (friend.isNewFriend()) {
        	holder.txt_unreadMsg.setTextColor(
        			context.getResources().getColor(R.drawable.red));
        }
        else {
        	holder.txt_unreadMsg.setTextColor(
        			context.getResources().getColor(R.drawable.gray));
        }
        
        if (friend.getLastMsgTime() != null) {
        	
        	holder.txt_timestamp.setText(String.format("%02d:%02d", 
        			friend.getLastMsgTime().getHours(),
        			friend.getLastMsgTime().getMinutes()));
        }
        else{
        	holder.txt_timestamp.setText("");
        }
        setVipIcon(friend, holder.img_vip, holder.img_vipYear);
		return convertView;
	}
	
	public class FriendsViewHolder{
        public ImageView img_avatar;
        public ImageView img_vip;
        public ImageView img_vipYear;
        public TextView txt_nickname;
        public TextView txt_profile;
        public TextView txt_unreadMsg;
        public TextView txt_timestamp;
    }

}
